/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.makrobioklub.action.web;

import cz.makrobioklub.action.ejb.ActionLocal;
import cz.makrobioklub.action.ejb.ActionRoles;
import cz.makrobioklub.action.entity.Action;
import cz.makrobioklub.action.entity.ActionPrice;
import java.util.LinkedList;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.fyx.linker.web.LinkerLocal;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.util.FyxUtility;
import org.fyx.web.context.WebContext;
import org.fyx.web.context.WebPage;

/**
 *
 * @author xljan
 */
@ManagedBean
@SessionScoped
public class ActionAdd {

    private Action action = new Action();
    private ActionPrice actionPrice = new ActionPrice();
    @EJB
    private ActionLocal actionBean;
    @EJB
    private MulaLocal mula;
    @EJB
    private LinkerLocal linker;

    @PostConstruct
    public void init() {
        FyxUtility.checkRole(ActionRoles.ACTION_WRITER);
        actionPrice = new ActionPrice();
        action = new Action();
        action.setActionPriceCollection(new LinkedList<ActionPrice>());
    }

    public Action getAction() {
        return action;
    }

    public void setAction(Action action) {
        this.action = action;
    }

    public ActionPrice getActionPrice() {
        return actionPrice;
    }

    public void setActionPrice(ActionPrice actionPrice) {
        this.actionPrice = actionPrice;
    }

    public void addPrice() {
        System.out.println("Add price");
        action.getActionPriceCollection().add(actionPrice);
        actionPrice = new ActionPrice();
    }
    
    public void removePrice(ActionPrice price){
        action.getActionPriceCollection().remove(price);
    }
    
    public void redirectToCopy(Action copyAction){
        action = copyAction;
        action.setIdAction(0);
        String url = "action/add.xhtml";
        WebPage.redirect(linker.getLink(url));
    }
    
    public void redirectToEdit(Action copyAction){
        action = copyAction;
        String url = "action/add.xhtml";
        WebPage.redirect(linker.getLink(url));
    }

    public void create(){
        actionBean.createAction(action);
        FacesMessage msg = new FacesMessage(mula.get("Action was created."),
                mula.get("Congratulations!"));
        WebContext.getMessages().add(msg);
        WebPage.redirect(linker.getLink("action/list.xhtml"));
        init();
    }
    
    public void save() {
        if(action.getIdAction() > 0){
            edit();
        }else{
            create();
        }
    }
    
    public void edit() {
        actionBean.editAction(action);
        FacesMessage msg = new FacesMessage(mula.get("Action was saved."),"");
        WebContext.getMessages().add(msg);
        WebPage.redirect(linker.getLink("action/list.xhtml"));
        init();
    }
    
    public Action getNewAction(){
        return new Action();
    }
}
